package com.itheima.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.itheima.user.sentinel.blockclasss.UserSentinelControllerHanlder;
import com.itheima.user.sentinel.fallback.GlobalFallBackHandler;
import com.itheima.user.sentinel.fallback.UserControllerFallback;
import com.itheima.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("user")
public class SentinelUserController {

    @Autowired
    UserService userService;

    //资源1
    @GetMapping(value = "/hello1")
    public String hello1() {
        return "Hello Sentinel1";
    }

    //资源2
    @GetMapping(value = "/hello2")
    public String hello2() {
        return "Hello Sentinel2";
    }

    //资源3
    @GetMapping(value = "/hello3")
    public String hello3() {
        userService.dosomething();
        return "Hello Sentinel3";
    }

    //资源4
    @GetMapping(value = "/hello4")
    public String hello4() {
        userService.dosomething();
        return "Hello Sentinel4";
    }

    //资源5
    @GetMapping(value = "/hello5/{flag}")
    public String hello5(@PathVariable("flag") int flag) throws InterruptedException {
        if (flag == 1){
            TimeUnit.SECONDS.sleep(3);
        }
        return "Hello Sentinel5";
    }

    //资源6
    @GetMapping(value = "/hello6/{flag}")
    @SentinelResource(value = "hello6",
            fallbackClass = GlobalFallBackHandler.class, fallback = "fallbackHandler")
    public String hello6(@PathVariable("flag") int flag) throws InterruptedException {
        if (flag == 1){
            int i =1/0;
        }
        return "Hello Sentinel6";
    }


    //资源  热点参数限流
    @SentinelResource(value = "hello66", blockHandler = "hello66BlockHandler")
    @GetMapping(value = "/hello66")
    public String hello66(
            @RequestParam(required = false) String a,
            @RequestParam(required = false)  Integer b) {
        return "Hello Sentinel66 a="+a+" b="+b;
    }


    @SentinelResource(value = "hello7",
            blockHandler = "hello7BlockHandler",
            blockHandlerClass = UserSentinelControllerHanlder.class,
            fallback = "hello7Fallback",
            fallbackClass = UserControllerFallback.class
    )
    @GetMapping(value = "/hello7")
    public String hello7(@RequestParam("flag") int flag){
        int i =1/0;
        return "Hello Sentinel7";
    }




}
